﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aandelendelen.Ad;

namespace Aandelendelen.Controls
{
	public partial class AdvertiseOptionsControl : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			this.RenderOptions();
		}

		private void RenderOptions()
		{
			AdvertiseOptionCollection advertiseOptions = AdvertiseCache.GetAdvertiseOptions();

			foreach (AdvertiseOption option in advertiseOptions)
			{
				Label breakLable = new Label();
				breakLable.Text = "<br \\>";
				this.Panel1.Controls.Add(breakLable);

				this.RenderOption(option);

			}
		}

		private void RenderOption(AdvertiseOption option)
		{
			Label optionTitle = new Label();
			optionTitle.Text = "\u25BA " + option.Title;
			optionTitle.CssClass = "advertiseoptiontitle";

			this.Panel1.Controls.Add(optionTitle);

			Panel optionDetails = new Panel();
			optionDetails.Style.Add("margin-left", "32px");

			Label optionDescription = new Label();
			optionDescription.Text = option.Description;
			optionDetails.Controls.Add(optionDescription);

			optionDetails.Controls.Add(this.RenderCostTable(option));

			this.Panel1.Controls.Add(optionDetails);
		}

		private Table RenderCostTable(AdvertiseOption option)
		{
			Table costTable = new Table();
			costTable.Attributes["border"] = "1";
			TableRow headerRow = new TableRow();

			TableCell headerCellDesc = new TableCell();
			headerCellDesc.Text = option.AdvertiseCosts.TitleDesc;
			headerCellDesc.Attributes["align"] = "right";
			headerRow.Cells.Add(headerCellDesc);

			TableCell headerCellCost = new TableCell();
			headerCellCost.Text = option.AdvertiseCosts.TitleCost;
			headerCellCost.Attributes["align"] = "right";
			headerRow.Cells.Add(headerCellCost);

			costTable.Rows.Add(headerRow);

			foreach (AdvertiseCost cost in option.AdvertiseCosts)
			{
				costTable.Rows.Add(this.RenderCostRow(cost));
			}

			return costTable;
		}

		private TableRow RenderCostRow(AdvertiseCost cost)
		{
			TableRow row = new TableRow();

			TableCell rangeCell = new TableCell();
			rangeCell.Text = cost.Description;
			rangeCell.Attributes["align"] = "right";
			row.Cells.Add(rangeCell);

			TableCell costCell = new TableCell();
			costCell.Text = cost.CostString;
			costCell.Attributes["align"] = "right";
			row.Cells.Add(costCell);

			return row;
		}

	}
}